﻿using System;
using System.Windows;
using System.Windows.Media;
using NetBay.Core.BusinessEntities;

namespace Rio.Framework.Pages.CriteriaControls
{

    public partial class BooleanIndexControl 
    {

        /// <summary>
        /// Get or set the control value
        /// </summary>
        public override sealed string InputValue
        {
            get 
            {
                string l_bReturnValue = "0";
                if (CheckBoxInput.IsChecked != null && CheckBoxInput.IsChecked.Value)
                {
                    l_bReturnValue = "1";
                }
                return l_bReturnValue;
            }
            set
            {
                if (value != null)
                {
                    if ("1".Equals(value.ToLower()))
                    {
                        CheckBoxInput.IsChecked = true;
                    }
                    else
                    {
                        CheckBoxInput.IsChecked = false;
                    }
                }
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BooleanIndexControl"/> class.
        /// </summary>
        public BooleanIndexControl()
        {
            InitializeComponent();

            _originalBorderBrush = this.CheckBoxInput.BorderBrush as LinearGradientBrush;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="BooleanIndexControl"/> class.
        /// </summary>
        /// <param name="P_oIndex">Index of the p_o.</param>
        /// <param name="P_szValue">The P_SZ value.</param>
        public BooleanIndexControl(Index P_oIndex, string P_szValue = null)
            : this()
        {
            CurrentIndex = P_oIndex;
            CheckBoxInput.Content = CurrentIndex.IndexLabel;

            //if a value is selected at the start 
            InputValue = P_szValue;

            //Mandatory indication
            LabelMandatory.Visibility = CurrentIndex.IsMandatory ? Visibility.Visible : Visibility.Collapsed;
        }

        /// <summary>
        /// Clear input
        /// </summary>
        public override void Clear()
        {
            CheckBoxInput.IsChecked = false;
        }

        /// <summary>
        /// Handles the Click event of the buttonDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            OnRemoveControlClick(new EventArgs());
        }

        /// <summary>
        /// Set the default index value
        /// </summary>
        public override void SetDefaultValue()
        {
            if (!string.IsNullOrEmpty(this.CurrentIndex.DefaultValue))
            {
                this.CheckBoxInput.IsChecked = Convert.ToBoolean(this.CurrentIndex.DefaultValue);
            }
        }

        /// <summary>
        /// Used to set border color of input control
        /// </summary>
        public override void ChangeBorderColor(Color color)
        {
            this.CheckBoxInput.BorderBrush = new SolidColorBrush(color);
        }

        public override void RevertBorderColor()
        {
            this.CheckBoxInput.BorderBrush = _originalBorderBrush;
        }

        /// <summary>
        /// Hide mandatory label
        /// </summary>
        public override void DisableMandatoryLabel()
        {
            this.LabelMandatory.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Set the max lenght of the control
        /// </summary>
        public override void SetMaxLength()
        {
        }

        /// <summary>
        /// Hides Index label name
        /// </summary>
        public override void DisableIndexLabel()
        {
        }

        /// <summary>
        /// Hides filter button
        /// </summary>
        public override void DisableFilter()
        {
        }

        /// <summary>
        /// Activate Removing feature
        /// </summary>
        public override void ActiveRemove()
        {
            this.DeleteButton.Visibility = Visibility.Visible;
        }

    }

}
